package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.pojo.TcHomework;
import com.zhentao.service.TcHomeworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("tea/homework")
public class HomeworkController {

    @Autowired
    TcHomeworkService homeworkService;

    /**
     * 获取全部作业
     */
    @GetMapping("list")
    public List<TcHomework> list(){
        return homeworkService.list();
    }

    /**
     * 根据ID获取作业详情
     */
    @GetMapping("get/{id}")
    public TcHomework getById(@PathVariable Integer id){
        return homeworkService.getById(id);
    }

    /**
     * 添加作业
     */
    @PostMapping("add")
    public Map<String, Object> add(@RequestBody TcHomework homework){
        Map<String, Object> result = new HashMap<>();
        try {
            homework.setCreateTime(new Date());
            boolean success = homeworkService.save(homework);
            result.put("success", success);
            result.put("message", success ? "添加成功" : "添加失败");
            if (success) {
                result.put("data", homework);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 更新作业
     */
    @PutMapping("update")
    public Map<String, Object> update(@RequestBody TcHomework homework){
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = homeworkService.updateById(homework);
            result.put("success", success);
            result.put("message", success ? "更新成功" : "更新失败");
            if (success) {
                result.put("data", homework);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 删除作业
     */
    @DeleteMapping("delete/{id}")
    public Map<String, Object> delete(@PathVariable Integer id){
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = homeworkService.removeById(id);
            result.put("success", success);
            result.put("message", success ? "删除成功" : "删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 根据班级ID获取作业
     */
    @GetMapping("classHomework/{classId}")
    public List<TcHomework> getClassHomework(@PathVariable Integer classId){
        QueryWrapper<TcHomework> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_id", classId);
        queryWrapper.orderByDesc("create_time");
        return homeworkService.list(queryWrapper);
    }

    /**
     * 根据发布者ID获取作业
     */
    @GetMapping("myHomework/{publisherUserId}")
    public List<TcHomework> getMyHomework(@PathVariable Integer publisherUserId){
        QueryWrapper<TcHomework> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("publisher_user_id", publisherUserId);
        queryWrapper.orderByDesc("create_time");
        return homeworkService.list(queryWrapper);
    }

    /**
     * 获取未截止的作业
     */
    @GetMapping("activeHomework")
    public List<TcHomework> getActiveHomework(){
        QueryWrapper<TcHomework> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("deadline", new Date());
        queryWrapper.orderByDesc("create_time");
        return homeworkService.list(queryWrapper);
    }

    /**
     * 获取已截止的作业
     */
    @GetMapping("expiredHomework")
    public List<TcHomework> getExpiredHomework(){
        QueryWrapper<TcHomework> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("deadline", new Date());
        queryWrapper.orderByDesc("create_time");
        return homeworkService.list(queryWrapper);
    }

    /**
     * 搜索作业
     */
    @GetMapping("search")
    public List<TcHomework> searchHomework(@RequestParam String keyword){
        QueryWrapper<TcHomework> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", keyword).or().like("content", keyword);
        queryWrapper.orderByDesc("create_time");
        return homeworkService.list(queryWrapper);
    }

    /**
     * 批量删除作业
     */
    @DeleteMapping("batchDelete")
    public Map<String, Object> batchDelete(@RequestBody List<Integer> ids){
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = homeworkService.removeByIds(ids);
            result.put("success", success);
            result.put("message", success ? "批量删除成功" : "批量删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "批量删除失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 设置是否允许修改提交
     */
    @PutMapping("setAllowModify/{id}")
    public Map<String, Object> setAllowModify(@PathVariable Integer id, @RequestParam Integer isAllowModify){
        Map<String, Object> result = new HashMap<>();
        try {
            TcHomework homework = homeworkService.getById(id);
            if (homework != null) {
                homework.setIsAllowModify(isAllowModify);
                boolean success = homeworkService.updateById(homework);
                result.put("success", success);
                result.put("message", success ? "设置成功" : "设置失败");
            } else {
                result.put("success", false);
                result.put("message", "作业不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "设置失败：" + e.getMessage());
        }
        return result;
    }
}
